<?php

namespace api\modules\user\models;

use common\exceptions\ModelHttpException;
use Yii;
use yii\data\ActiveDataProvider;
use common\models\user\UserFollower;
use yii\db\Exception;

class FansSearch extends UserFollower {

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params = []) {

        $query = static::find()->where([
            'followed_user_id' => Yii::$app->user->id,
        ])->orderBy('id desc');
        $query->with(['user']);

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        return $dataProvider;
    }

    public function searchForUser($params=[]){
        if(!isset($params['user_id'])){
            throw new ModelHttpException('user_id 不能为空');
        }

        $query = static::find()->where([
            'followed_user_id' => $params['user_id'],
        ])->orderBy('id desc');
        $query->with(['user']);

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        return $dataProvider;
    }

    public function fields() {
        return [
            'id',
            'fans_info'=>'user',
            'created_at'
        ];
    }

}